const express = require("express");
const router = express.Router();
let { myroomModel, maint_jdModel, maintModel } = require('../models/model')


//获取租住信息
router.get('/getMyRoom', async (req, res) => {
    let myRooms = await myroomModel.find();
    res.send({
        code: 200,
        myRooms
    })
})
//设置默认信息
router.post('/setDefault', async (req, res) => {
    let { _id } = req.body;
    console.log(_id)
    await myroomModel.updateMany({}, { def_Status: 0 });

    await myroomModel.findByIdAndUpdate(_id, { def_Status: 1 });
    res.send({
        code: 200,
        msg: '设置成功'
    })
})
//添加住户信息
router.post('/addMyRoom', async (req, res) => {
    let { isObj } = req.body;
    console.log(isObj)
    await myroomModel.create(isObj);
    res.send({
        code: 200,
        msg: '添加成功'
    })
})


//获取维修信息
router.get('/getMaint', async (req, res) => {
    let maints = await maintModel.find();
    res.send({
        code: 200,
        maints
    })
})

//添加维修信息
router.post('/addMaint', async (req, res) => {
    let { MaintInfo } = req.body;
    console.log(MaintInfo)
    await maintModel.create(MaintInfo);
    res.send({
        code: 200,
        msg: '添加成功'
    })
})

const {ChatCompletion, setEnvVariable} = require("@baiducloud/qianfan");

// 设置百度云千帆的 API Key 和 Secret Key
setEnvVariable('QIANFAN_AK', '9lFqOXYm08vIooie9OpaiUIO');
setEnvVariable('QIANFAN_SK', 'ZW4BlEcjgHhTZDOxXtmsbiNVzB5irdxV');

const client = new ChatCompletion({Endpoint: 'completions_pro'});

// 处理聊天请求的路由
router.post('/chat', async (req, res) => {
    try {
        const { messages } = req.body;
        const resp = await client.chat({
            messages
        });
        res.json({ result: resp.result });
    } catch (error) {
        console.error('Error in chat:', error);
        res.status(500).json({ error: 'Internal Server Error' });
    }
});

module.exports = router;
